﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Castle.Services.Transaction;
using Meqs.Entity;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    [Transactional]
    public class QuestionTypesLogic : CommonLogic<QuestionTypes, int>, IQuestionTypesLogic
    {
        private readonly IQuestionItemsLogic _qiLogic;

        public QuestionTypesLogic(IQuestionItemsLogic _qiLogic)
        {
            this._qiLogic = _qiLogic;
        }

        #region Implementation of ICommonLogic<QuestionTypes, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(QuestionTypes entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(QuestionTypes entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int Delete(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("QuestionTypes.Delete", ht);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("QuestionTypes.Delete", ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public QuestionTypes FindById(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<QuestionTypes>("QuestionTypes.FindById", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int FindCount(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByCount("QuestionTypes.FindCount", ht);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<QuestionTypes> FindAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<QuestionTypes>("QuestionTypes.Find", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<QuestionTypes> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        // 删除此题目类别下的所有题目
        [Transaction(TransactionMode.Requires)]
        public bool DeleteQuestionsByReqClsId(int reqClsId, String schoolMark)
        {
            QuestionTypes reqCls = this.FindById(reqClsId, schoolMark);
            Hashtable ht = new Hashtable();
            ht.Add("TemplateID", reqCls.TemplateID);
            ht.Add("SchoolMark", schoolMark);
            IList<QuestionTypes> reqClsList = this.FindAll(ht);

            IList<Hashtable> needUpdateList = new List<Hashtable>();
            foreach (QuestionTypes item in reqClsList)
            {
                if (item.QtPosition > reqCls.QtPosition)
                {
                    item.QtPosition -= 1;
                    Hashtable tht = new Hashtable();
                    tht.Add("QuestionTypes", item);
                    tht.Add("SchoolMark", schoolMark);
                    needUpdateList.Add(tht);
                }
            }
            if (needUpdateList.Count > 0)
                BatchUpdate("QuestionTypes.UpdateQuestionTypes", needUpdateList);

            ht = new Hashtable();
            ht.Add("QtTypeID", reqClsId);
            ht.Add("SchoolMark", schoolMark);
            IList<QuestionItems> reqList = _qiLogic.FindAll(ht);
            if (reqList != null && reqList.Count > 0)
            {
                foreach (QuestionItems reqItem in reqList)
                {
                    int n = _qiLogic.Delete(reqItem.RID, schoolMark);
                    if (n < 1)
                        throw new Exception();
                }
            }

            return this.Delete(reqClsId, schoolMark) > 0 ? true : false;
        }

        public int AddReqCls(QuestionTypes obj, String schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TemplateID", obj.TemplateID);
            ht.Add("SchoolMark", schoolMark);
            IList<QuestionTypes> reqClsList = this.FindAll(ht);

            //if (reqClsList.Count < 1)
            //{
            //    obj.QtPosition = 0;
            //}
            //else
            //{
            //    int maxIndex = 0;
            //    foreach (QuestionTypes reqCls in reqClsList)
            //    {
            //        maxIndex = Math.Max(reqCls.QtPosition, maxIndex);
            //    }
            //    obj.QtPosition = maxIndex + 1;
            //}


            IList<Hashtable> needUpdateList = new List<Hashtable>();
            foreach (QuestionTypes item in reqClsList)
            {
                if (item.QtPosition >= obj.QtPosition)
                {
                    item.QtPosition += 1;
                    Hashtable tht = new Hashtable();
                    tht.Add("QuestionTypes", item);
                    tht.Add("SchoolMark", schoolMark);
                    needUpdateList.Add(tht);
                }
            }
            if (needUpdateList.Count > 0)
                BatchUpdate("QuestionTypes.UpdateQuestionTypes", needUpdateList);

            return this.CreateQuestionTypes(obj, schoolMark);
        }

        /// <summary>
        /// 大题排序
        /// </summary>
        public int SortReqCls(int stId, Hashtable ht, String schoolMark)
        {
            Hashtable htt = new Hashtable();
            htt.Add("TemplateID", stId);
            htt.Add("SchoolMark", schoolMark);
            IList<QuestionTypes> reqClsList = this.FindAll(htt);
            IList<Hashtable> needUpdateList = new List<Hashtable>();
            foreach (QuestionTypes item in reqClsList)
            {
                item.QtPosition = Convert.ToInt32(ht[item.RID.ToString()]);
                Hashtable tht = new Hashtable();
                tht.Add("QuestionTypes", item);
                tht.Add("SchoolMark", schoolMark);
                needUpdateList.Add(tht);
            }
            return BatchUpdate("QuestionTypes.UpdateQuestionTypes", needUpdateList);
        }

        // 复制指定类别下所有题目到目标样表
        [Transaction(TransactionMode.Requires)]
        public bool CopyReqClsToNewTable(int destStid, int reqClsId, String schoolMark)
        {
            int newRcId = 0;
            QuestionTypes rc = this.FindById(reqClsId, schoolMark);
            QuestionTypes newRc = new QuestionTypes();
            newRc.TemplateID = destStid;
            newRc.QtName = rc.QtName;
            newRc.QtPosition = rc.QtPosition;
            newRcId = this.CreateQuestionTypes(newRc, schoolMark);
            if (newRcId < 1)
                throw new Exception();

            Hashtable ht = new Hashtable();
            ht.Add("QtTypeID", reqClsId);
            ht.Add("SchoolMark", schoolMark);
            IList<QuestionItems> reqList = _qiLogic.FindAll(ht);
            foreach (QuestionItems reqItem in reqList)
            {
                bool b = CopyReqToNewTable(newRcId, reqItem.RID, schoolMark);
                if (!b)
                    throw new Exception();
            }

            return true;
        }

        /// <summary>
        /// 复制某类别下所有题目信息
        /// </summary>
        [Transaction(TransactionMode.Requires)]
        private bool CopyReqToNewTable(int destRcId, int reqId, String schoolMark)
        {
            int newReqId = 0;
            QuestionItems req = _qiLogic.FindById(reqId, schoolMark);
            QuestionItems newReq = new QuestionItems();
            newReq.QtTypeID = destRcId;
            newReq.QiOpts = req.QiOpts;
            newReq.QiContent = req.QiContent;
            newReq.QiType = req.QiType;
            newReq.QiScore = req.QiScore;
            newReq.QiPosition = req.QiPosition;
            newReqId = _qiLogic.CreateQuestionItems(newReq, schoolMark);
            if (newReqId < 1)
                throw new Exception();

            return true;
        }

        /// <summary>
        /// 插入题目类别
        /// </summary>
        public int CreateQuestionTypes(QuestionTypes questionType, string schoolMark)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("QuestionTypes", questionType);
                ht.Add("SchoolMark", schoolMark);

                int id = (int)InsertCustomer("QuestionTypes.InsertQuestionTypes", ht);
                return id;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        /// <summary>
        /// 更新题目类别
        /// </summary>
        public int UpdateQuestionTypes(QuestionTypes questionType, string schoolMark)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("QuestionTypes", questionType);
                ht.Add("SchoolMark", schoolMark);

                int id = (int)UpdateCustomer("QuestionTypes.UpdateQuestionTypes", ht);
                return id;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        /// <summary>
        /// 获取题类
        /// </summary>
        /// <param name="templateId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<QuestionTypeExtends> GetQtList(int templateId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TemplateID", templateId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<QuestionTypeExtends>("QuestionTypes.FindExtends", ht);
        }
    }
}
